﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QL_DTTN.KQT.BUS;
using QL_DTTN.DTO.DTO;
using DevExpress.XtraGrid.Views.Grid;
namespace QL_DTTN.KQT
{
    public partial class FrmCapNhatKetQuaThi : DevExpress.XtraEditors.XtraForm
    {
        public FrmCapNhatKetQuaThi()
        {
            InitializeComponent();
            _taiKhoan = new TaiKhoan();
        }
        private TaiKhoan _taiKhoan;

        public TaiKhoan TaiKhoan
        {
            set { _taiKhoan = value; }
        }
        private void FrmCapNhatKetQuaThi_Load(object sender, EventArgs e)
        {
            cbxTinh_Load();
            cbxHeDaoTao_Load();
            cbxKyThi_Load();
            cbxTinhP_Load();
            cbxTinhP_SelectedIndexChanged(sender, e);
            cbxTinh_SelectedIndexChanged(sender, e);
        }
        private void cbxTinh_Load()
        {
            DataTable dt = Tinh_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maTinh"] = 0;
            d["tenTinh"] = "Tất cả";
            dt.Rows.Add(d);
            cbxTinh.DataSource = dt;
            cbxTinh.DisplayMember = "tenTinh";
            cbxTinh.ValueMember = "maTinh";
            cbxTinh.SelectedIndex = dt.Rows.Count - 1;
          
        }
        private void cbxTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbxTinh.SelectedIndex != -1 && cbxTinh.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    DataTable dt = Truong_Bus.dataTable(Convert.ToInt32(cbxTinh.SelectedValue));
                    DataRow d = dt.NewRow();
                    d["maTruong"] = 0;
                    d["tenTruong"] = "Tất cả";
                    dt.Rows.Add(d);
                    cbxTruong.DataSource = dt;
                    cbxTruong.DisplayMember = "tenTruong";
                    cbxTruong.ValueMember = "maTruong";
                    cbxTruong.SelectedIndex = cbxTruong.Items.Count - 1;
                }
            }
            catch
            {

            }
        }
        private void cbxTinhP_Load()
        {
            DataTable dt = Tinh_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maTinh"] = 0;
            d["tenTinh"] = "Tất cả";
            dt.Rows.Add(d);
            cbxTinhP.DataSource = dt;
            cbxTinhP.DisplayMember = "tenTinh";
            cbxTinhP.ValueMember = "maTinh";
            cbxTinhP.SelectedIndex = dt.Rows.Count - 1;
        }
        private void cbxHeDaoTao_Load()
        {
            DataTable dt = HeDaoTao_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maHeDaoTao"] = 0;
            d["tenHeDaoTao"] = "Tất cả";
            dt.Rows.Add(d);
            cbxHeDaoTao.DataSource = dt;
            cbxHeDaoTao.DisplayMember = "tenHeDaoTao";
            cbxHeDaoTao.ValueMember = "maHeDaoTao";
            cbxHeDaoTao.SelectedIndex = cbxHeDaoTao.Items.Count - 1;
        }
        private void cbxKyThi_Load()
        {
            DataTable dt = KyThi_Bus.dataTable();
            cbxKyThi.DataSource = dt;
            cbxKyThi.ValueMember = "maKyThi";
            cbxKyThi.DisplayMember = "tenKyThi";
        }

        private void cbxKyThi_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = MonThi_Bus.dataTable(Convert.ToInt64(cbxKyThi.SelectedValue), Convert.ToInt16(cbxHeDaoTao.SelectedValue));
                DataRow d = dt.NewRow();
                d["MaMonThi"] = 0;
                d["TenMon"] = "Tất cả";
                dt.Rows.Add(d);
                cbxMonThi.DataSource = dt;
                cbxMonThi.ValueMember = "MaMonThi";
                cbxMonThi.DisplayMember = "TenMon";
                cbxMonThi.SelectedIndex = dt.Rows.Count - 1;
            }
            catch
            {
            }
        }

        private void cbxTinhP_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbxTinh.SelectedIndex != -1 && cbxTinh.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    DataTable dt = DiemThi_Bus.dataTable(Convert.ToInt32(cbxTinhP.SelectedValue));
                    DataRow d = dt.NewRow();
                    d["maDiemThi"] = 0;
                    d["tenDiemThi"] = "Tất cả";
                    d["diaChi"] = "";
                    d["soDienThoai"] = "";
                    dt.Rows.Add(d);
                    lueDiemThi.Properties.DataSource = dt;
                    lueDiemThi.Properties.DisplayMember = "tenDiemThi";
                    lueDiemThi.Properties.ValueMember = "maDiemThi";
                    lueDiemThi.ItemIndex = dt.Rows.Count - 1;
                }
            }
            catch
            {

            }
        }

        private void lueDiemThi_EditValueChanged(object sender, EventArgs e)
        {
            DataRowView dr = (DataRowView)lueDiemThi.GetSelectedDataRow();
            var a = Convert.ToInt64(dr["maDiemThi"]);
            DataTable dt = PhongThi_Bus.dataTable(a);
            DataRow d = dt.NewRow();
            d["maPhongThi"] = 0;
            d["tenPhongThi"] = "Tất cả";
            dt.Rows.Add(d);
            cbxPhongThi.DataSource = dt;
            cbxPhongThi.DisplayMember = "tenPhongThi";
            cbxPhongThi.ValueMember = "maPhongThi";
            cbxPhongThi.SelectedIndex = dt.Rows.Count - 1;
        }

        private void gvwKetQua_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            DTO.DTO.KetQuaThi ts = new DTO.DTO.KetQuaThi();
            GridView view = sender as GridView;
            if (view.GetRowCellValue(e.RowHandle, view.Columns["KetQua"]).ToString().Trim()==null)
            {
                MessageBox.Show("Chưa nhập kết quả");
                return;
            }
            double kq = 0;
            if (!double.TryParse(view.GetRowCellValue(e.RowHandle, view.Columns["KetQua"]).ToString(),out kq))
            {
                MessageBox.Show("Kết quả không đúng định dạng");
                return;
            }
            ts.SoBaoDanh = view.GetRowCellValue(e.RowHandle, view.Columns["SoBaoDanh"]).ToString();
            ts.MaMon =Convert.ToInt16(view.GetRowCellValue(e.RowHandle, view.Columns["MaMon"]));
            ts.KetQua =kq;
            ts.TenTaiKhoan = _taiKhoan.TenTaiKhoan;
            if (!KetQuaThi_Bus.CapNhat(ts))
            { 
                MessageBox.Show("Cập nhật thất bại");
                cbxTimKiem_Click(sender, e);     
            }
        }
        private void cbxTimKiem_Click(object sender, EventArgs e)
        {
            short maTinh = cbxTinh.SelectedValue == null ? Convert.ToInt16(0) : Convert.ToInt16(cbxTinh.SelectedValue);
            string maTruong = cbxTruong.SelectedValue.ToString();
            short maHeDaoTao = cbxHeDaoTao.SelectedValue == null ? Convert.ToInt16(0) : Convert.ToInt16(cbxHeDaoTao.SelectedValue);
            long maKyThi = cbxKyThi.SelectedValue == null ? Convert.ToInt16(0) : Convert.ToInt16(cbxKyThi.SelectedValue);
            long maMonThi = cbxMonThi.SelectedValue == null ? Convert.ToInt16(0) : Convert.ToInt16(cbxMonThi.SelectedValue);
            short maTinhD = cbxTinhP.SelectedValue == null ? Convert.ToInt16(0) : Convert.ToInt16(cbxTinhP.SelectedValue);
            DataRowView dr = (DataRowView)lueDiemThi.GetSelectedDataRow();
            long maDiemThi = dr["maDiemThi"] == null ? Convert.ToInt16(0) : Convert.ToInt16(dr["maDiemThi"]);
            long maPhongThi = cbxPhongThi.SelectedValue == null ? Convert.ToInt16(0) : Convert.ToInt16(cbxPhongThi.SelectedValue);
            DataTable dt = ThiSinh_Bus.DataDiemThi(txtKey.Text.Trim(), maTinh, maTruong, maHeDaoTao, maKyThi, maMonThi, maTinhD, maDiemThi, maPhongThi);
            gctKetQua.DataSource = dt;
        }

    }
}